<template>
  <div>
    <!-- 面包屑导航部分 -->
    <div class="breadcrumb">
    <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    </div>

    <el-card class="el-card">
      <el-alert title="注意：只允许为第三级分类设置相关参数！" type="warning" show-icon :closable="false" class="el-alert"></el-alert>
<!-- 选择商品部分 -->
      <el-row class="cat_opt">
        <el-col>
          <span>选择商品分类</span>
          <el-cascader
    v-model="selectedCateKeys"
    :options="catesList"
    :props="cateProps"
    @change="parentCateChanged" clearable>
          </el-cascader>
        </el-col>
      </el-row>

<!-- tab页签区域 -->
    <el-tabs v-model="activeName" @tab-click="handleTabClick">
      <el-tab-pane label="动态参数" name="many">
        <el-row>
          <el-button type="primary" :disabled="selectedCateKeys.length === 0" @click="showAddDialog">添加参数</el-button>
        </el-row>
<!-- 动态参数部分 -->
        <el-table :data="manyTableData" border stripe>
          <!-- 展开列 -->
          <el-table-column type="expand">
            <template slot-scope="scope">
              <div v-if="scope.row.attr_vals.length!=0">
              <el-tag class="el-tag" type="success" v-for="(item,index) in scope.row.attr_vals.split(' ')" :key="index" closable>
                {{item}}
              </el-tag>

              <el-input
                class="input-new-tag"
                v-if="scope.row.inputVisible"
                v-model="scope.row.inputValue"
                ref="saveTagInput"
                size="small"
                @keyup.enter.native="handleInputConfirm(scope.row)"
                @blur="handleInputConfirm(scope.row)">
              </el-input>
<el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">
  + New Tag</el-button>
              </div>
            </template>

          </el-table-column>
          <!-- 索引列 -->
          <el-table-column type="index"></el-table-column>
          <!-- 数据展示列 -->
          <el-table-column prop="attr_name" label="参数名称"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
               <el-button  type="primary" icon="el-icon-edit" plain size="mini">编辑</el-button>
               <el-button type="danger" icon="el-icon-delete" plain size="mini" @click="showDeleteDialog(scope.row.attr_id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

      </el-tab-pane>
      <el-tab-pane label="静态属性" name="only">
        <el-row>
          <el-button type="primary" :disabled="selectedCateKeys.length === 0" @click="showAddDialog1">添加属性</el-button>
        </el-row>
<!-- 静态属性部分 -->
        <el-table :data="onlyTableData" border stripe>
           <el-table-column type="expand">
            <template slot-scope="scope">
              <el-tag type="success" v-for="(item,index) in scope.row.attr_vals.split(',')" :key="index">
                {{item}}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="attr_name" label="静态属性"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
               <el-button  type="primary" icon="el-icon-edit" plain size="mini" >编辑</el-button>
               <el-button type="danger" icon="el-icon-delete" plain size="mini" @click="showDeleteDialog(scope.row.attr_id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

      </el-tab-pane>
    </el-tabs>

    </el-card>

<!-- 添加动态参数部分对话框 -->

<el-dialog
  title="添加动态参数"
  :visible.sync="addDialogVisable"
  width="70%"
  >
  <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px" class="demo-ruleForm">

  <el-form-item label="动态参数" prop="attr_name">
    <el-input v-model="addForm.attr_name"></el-input>
  </el-form-item>

  </el-form>
<!-- 这是取消和确定的两个按钮 -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisable= false">取 消</el-button>
    <el-button type="primary" @click="addMany">确 定</el-button>
  </span>
</el-dialog>

<!-- 添加静态属性部分对话框 -->
<el-dialog
  title="添加静态属性"
  :visible.sync="addDialogVisable1"
  width="70%"
  >
  <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px" class="demo-ruleForm">

  <el-form-item label="静态属性" prop="attr_name">
    <el-input v-model="addForm.attr_name"></el-input>
  </el-form-item>

  </el-form>
<!-- 这是取消和确定的两个按钮 -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisable1= false">取 消</el-button>
    <el-button type="primary" @click="addOnly">确 定</el-button>
  </span>
</el-dialog>

<!-- 删除参数或属性的对话框 -->
<el-dialog
  title="确认删除吗"
  :visible.sync="deleteDialogVisable"
  width="70%"
  >
 <em class="el-icon-warning"></em>此操作将永久删除该分类，是否继续？
<!-- 这是取消和确定的两个按钮 -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="deleteDialogVisable = false">取 消</el-button>
    <el-button type="primary" @click="deleteParam">确 定</el-button>
  </span>
</el-dialog>
  </div>

</template>




<script>
export default {
  data(){
    return {
      catesList:[],
      cateProps:{
        value:'cat_id',
        label:'cat_name',
        children:'children'
      },
      selectedCateKeys:[],
      activeName:'many',
      manyTableData:[],
      onlyTableData:[],
      // 添加动态参数
      addDialogVisable:false,
      editDialogVisable:false,
      // 添加静态属性
      addDialogVisable1:false,
      deleteDialogVisable:false,
      inputVisible:false,
      inputValue:'',
      deleteParamID:'',
      addForm:{
        attr_name:''
      },
      addFormRules:{
       attr_name: [{required:true,message:'输入的动态参数不能为空',trigger:"blur"}]
      }
    }
  },
  created(){
    this.getCatesList();
  },
  computed:{
     cateId(){
      if( this.selectedCateKeys.length === 3){
        return Number(this.selectedCateKeys[2])
      }
      return null;
    }
  },
  methods:{
    async getCatesList(){
      const {data:val} = await this.$http.get('categories');
      if(val.meta.status != 200){
        return  this.$message.error("获取分类信息失败,请重新刷新！");
      }
       this.catesList = val.data;

    },
    async parentCateChanged(){
      console.log(this.selectedCateKeys);
      // 证明选中的是三级分类
      if(this.selectedCateKeys.length != 3){
        this.selectedCateKeys = [];
        this.manyTableData = [],
        this.onlyTableData = []
      }
      this.getParamsData();

    },
    async getParamsData(){
      const {data:res} = await this.$http.get(`categories/${this.cateId}/attributes`,
      {
        params:{sel:this.activeName}
      });
      console.log(res);
      if(res.meta.status != 200){
        return this.$message.error("获取分类参数出错！");
      }
      res.data.forEach(item =>{
        item.inputVisible = false;
        item.inputValue = ''
      })
      console.log(res.data);
      if(this.activeName === 'many'){
        this.manyTableData = res.data;
      }else{
        this.onlyTableData = res.data;
      }


    },
    handleTabClick(){
      console.log(this.activeName);
      this.getParamsData();

    },
    showAddDialog(){
      this.addDialogVisable = true;
      this.addForm.attr_name = ''
    },
    showAddDialog1(){
      this.addDialogVisable1 = true;
      this.addForm.attr_name = ''
    },
    showDeleteDialog(id){
      this.deleteParamID = id;
      this.deleteDialogVisable = true;
    },
    async addMany(){
      this.$refs.addFormRef.validate(valid =>{
        if(!valid){
          return this.$message.error("添加动态属性失败")
        }
      })
      this.addDialogVisable = false;
      const {data:val} = await this.$http.post(`categories/${this.selectedCateKeys[2]}/attributes`,{
        attr_name:this.addForm.attr_name,
        id:this.selectedCateKeys[2],
        attr_sel:'many',
        attr_vals:'giao 药水哥'
      });
      if(val.meta.status != 201){
        this.addDialogVisable = false;
        return this.$message.error("添加动态属性失败");
      }
      this.addDialogVisable = false;
      this.getParamsData();
      return this.$message.success("添加动态属性成功")
    },
    async addOnly(){
      this.$refs.addFormRef.validate(valid1 =>{
        if(!valid1){
          return this.$message.error("添加动态属性失败")
        }
      })
      this.addDialogVisable1 = false;
      const {data:val} = await this.$http.post(`categories/${this.selectedCateKeys[2]}/attributes`,{
        attr_name:this.addForm.attr_name,
        id:this.selectedCateKeys[2],
        attr_sel:'only',
        attr_vals:'测试属性的值,值1,值2'
      });
      if(val.meta.status != 201){
        this.addDialogVisable1 = false;
        return this.$message.error("添加静态属性失败");
      }
      this.addDialogVisable1 = false;
      this.getParamsData();
      return this.$message.success("添加静态属性成功")
    },
    async deleteParam(){
      this.deleteDialogVisable = false;
      const {data:val} = await this.$http.delete(`categories/${this.selectedCateKeys[2]}/attributes/${this.deleteParamID}`);
      if(val.meta.status != 200){
        this.deleteDialogVisable = false;
        return this.$message.error("删除失败");
      }
      this.deleteDialogVisable = false;
      this.getParamsData();
      return this.$message.success("删除成功")
    },


    // 通过输入框来添加参数或属性的值
     showInput(row) {
        row.inputVisible = true;
         this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },

      handleInputConfirm(row) {

        row.inputVisible = false;
        row.inputValue = ''
      }




  }
}
</script>

<style scoped>
  .el-tag{
    margin: 10px;
  }
  .input-new-tag{
    width:100px;
  }
  .el-card{
    margin-top:15px
  }
  .el-alert{
    margin-bottom:10px
  }
</style>
